<?php
namespace Admin\Controller;
use \Com\Tree;

class NavController extends \Common\Controller\BaseController{

	protected $nav = null;    //导航模型

	public function _initialize(){
		parent::_initialize();
		$this->nav = D('CommonNav');
		
		$nav = $this->nav->getListByMap();
		
		$top_nav = array(
			array('name'=>'后台导航','url'=>U('Admin/Nav/index'),'tab'=>array('index')),
			//array('name'=>'会员后台','url'=>U('Admin:Nav/forenav'),'tab'=>'forenav'),
			//array('name'=>'快捷链接','url'=>U('Admin/Nav/pluslist'),'tab'=>array('pluslist')),
			//array('name'=>'添加快捷链接','url'=>U('Admin/Nav/addplus'),'tab'=>array('addplus')),
			//array('name'=>'添加导航','url'=>U('Admin/Nav/addnav'),'tab'=>array('addnav','editnav')),
		);
		$this->assign('top_nav',$top_nav);
		$this->assign('nav',$nav);
	}

    public function index() {
		$wherearr['type'] = 'admin';
		$array = array();
		
		$list = $this->nav->getListByMap($wherearr,"100","sort asc");
		foreach($list as $k => $v){
			if($v['status'] == '1'){
				$v['status'] = "<span class=\"label label-success\">开启</span>";
			}elseif($v['status'] == '0'){
				$v['status'] = "<span class=\"label label-warning\">禁用</span>";
			}
			$v['add_children'] = "<a href='".U('Admin:Nav/addnav',array('id'=>$v['id']))."' class='btn btn-sm btn-primary'>添加子栏目</a>";
			$v['edit'] = "<a href='".U('Admin/Nav/editnav',array('id'=>$v['id']))."' class='btn btn-sm btn-primary'>编辑</a>";
			$array[] = $v;
		}
		
		$str  = "<tr>
		  <td width='60' align='center'><label class='checkbox'><input type='checkbox' name='id' value='\$id'>\$id</label></td>
		  <td>\$spacer \$name</td>
		  <td width='60'>\$status</td>
		  <td width='260'>
			<div class='btn-group'>\$add_content \$add_children \$edit \$delete</div>
		  </td>
		</tr>";
		
  		$Tree = new Tree();
		$Tree->icon = array('&nbsp;&nbsp;│ ','&nbsp;&nbsp;├─ ','&nbsp;&nbsp;└─ ');
		$Tree->nbsp = '&nbsp;&nbsp;';
		$Tree->init($array);
		$html_tree = $Tree->get_tree(0, $str);
		$this->assign('html_tree',$html_tree);
		//$mune = list_to_tree($list);

		//$data = array('list'=>$mune);
		//$this->assign($data);
    	$this->display();
    }

    public function addnav(){
    	$id = I('id');   //父ID
    	if(IS_POST){

    		$data = $this->nav->create($data);
    		$result = $this->nav->addNav($data);
    		if($result){
    			$this->success("菜单添加成功！",U('Admin/Nav/index'));
    		}else{
    			$this->error("菜单添加失败！");
    		}
    	}else{
    		$this->assign('pid',$id);
    		$this->display('editnav');
    	}
    }

    public function editnav(){
    	$id = I('get.id','','trim');   //当前栏目ID
    	if(IS_POST){
    		$data = $this->nav->create($data);
    		$wherearr['id'] = $id;
    		$result = $this->nav->saveNav($wherearr,$data);
    		if($result){
    			$this->success("菜单修改成功！",U('Admin/Nav/index'));
    		}else{
    			$this->error("菜单修改失败！");
    		}
    	}else{
    		$data = $this->nav->where('id='.$id)->find();
    		$this->assign($data);
    		$this->display('editnav');
    	}
    }

    public function delnav(){
    	$id = I('post.id');

		$result = $this->nav->delNav($id);
    	if($result){
    		$this->success("删除成功！");
    	}else{
    		$this->error("删除失败！");
    	}
    }

    public function forenav(){
		$wherearr['type'] = 'foren';
		
		$list = $this->nav->getListByMap($wherearr);
		$mune = list_to_tree($list);

		$data = array('list'=>$mune);
		$this->assign($data);
    	$this->display('index');
    }

	/**
	*  name   : 编辑后台首页快捷方式
	*  
	*/
	public function addplus(){
		$list = F('Admin_Plus');
		$id = I('id','','trim');
		if(IS_POST){
			if(!$id){
				$id = time();
			}
			$list[$id] = array(
				'id'   => $id,
				'name'   => I('name','','trim'),
				'url'   => I('url','','trim'),
				'icon'   => I('icon','','trim'),
			);
			$result = F('Admin_Plus',$list);
			$this->success(L('ACTION SUCCESS'),U('Admin/Nav/pluslist'));
			/*if($result){
				$this->success(L('ACTION SUCCESS'),U('Admin/Nav/pluslist'));
			}else{
				$this->error(L('ACTION ERROR'));
			}*/
		}else{
			$this->assign($list[$id]);
			$this->display();
		}
	}

	/**
	*  name   : 编辑后台首页快捷方式
	*  
	*/
	public function pluslist(){
		$list = F('Admin_Plus');
		$this->assign('list',$list);
		$this->display();
	}
	public function delplus(){
		$list = F('Admin_Plus');
		$id = $_GET['id'];
		unset($list[$id]);
		$result = F('Admin_Plus',$list);
		if($result){
			$this->success(L('ACTION SUCCESS'),U('Admin/Nav/pluslist'));
		}else{
			$this->error(L('ACTION ERROR'));
		}
	}
}